<?php
/**
 * 节点管理
 * 
 * @author Lee<a605333742@gmail.com>
 * @date    2018-07-03
 */
namespace app\admin\controller;
use think\Db;

class Node extends Common{    
    public function __construct(\think\Request $request = null) {
        parent::__construct($request);
    }
    
    /*
     * 节点列表
     * 
     * @return #
     */
    public function index(){
        $condition['status']=1;
        $pageSize=15;
        $order=array("path"=>'ASC');
        $this->list    =$this->nodeModel->getNodeList($condition,'',$pageSize,$order,"*");
        $this->assign('list',$this->list);
        return $this->fetch();
    }
    
    /*
     * 添加节点
     * 
     * @return #
     */
    public function nodeEdit(){
        $condition=array(
            'level' =>0,
            'status'=>1
        );
        $this->list=$this->nodeModel->getNodeList($condition,1,'','id ASC','id,name');
        $this->assign('list',$this->list);        
        if(isset($this->data['id'])&&!empty($this->data['id'])){
            $field="id,name,controller,action,p_id,ord";
            $nodeInfo=$this->nodeModel->getOneNode(array("id"=>$this->data['id']),$field);            
            $this->assign('nodeInfo',$nodeInfo);
        }
        if(isset($this->data['sign'])&&$this->data['sign']=='edit'){
            //添加新的节点
            $condition=array(
                'id'        =>trim($this->data['id'])??'',
                'name'      =>trim($this->data['name']),
                'controller'=>trim($this->data['controller']),
                'action'    =>trim($this->data['action'])??trim($this->data['action']),
                'level'     =>(trim($this->data['p_id'])==0)?0:1,
                'p_id'      =>trim($this->data['p_id']),
                'ord'       =>trim($this->data['ord'])??99,
                'add_time'  =>time(),
                'edit_time' =>time(),
                'add_a_id'  =>session('user.id'),
                'edit_a_id' =>session('user.id')
            );  
            
            if(empty($condition['id'])){
                $condition1['name']         =$condition['name'];
                $condition2=array();
                if($condition['level']==0){
                    $condition2['controller']   =$condition['controller'];
                }
                if(Db::name("Node")->where($condition1)->whereOr($condition2)->find()){
                    ret_msg(10007);
                }
            }
            $result=$this->nodeModel->nodeEdit($condition);    
            if($condition['level']==0){
                //编辑的是顶级节点
                $map['path']=$result.'_';
            }else{
                //编辑的是次级节点,父节点的path
                $where['id']=$condition['p_id'];
                $field1     ='path';
                $pPath      =$this->nodeModel->getOneNode($where,$field1);
                $map['path']=$pPath['path'].$result."_";
            }
            if(empty($condition['id'])){
                //新增
                $map['id']=$result;
                if(!$this->nodeModel->nodeEdit($map)){
                    ret_msg(10005);
                }
                ret_msg(20000);
            }else{
                //编辑
                $where1=array(
                        'id'        =>$condition['id'],
                        'edit_time' =>time()+1,
                    );
                if($condition['level']==0){
                    $where1['path'] =$condition['id'].'_';
                }else{
                    $where1['path'] =$pPath['path'].$condition['id']."_";
                }
                if(!$this->nodeModel->nodeEdit($where1)){
                    ret_msg(10006);
                }
                ret_msg(20001);
            }
            $this->redirect('index');
        }        
        return $this->fetch();
    }
    
    /*
     * 获取控制器
     * 
     * @param int $id 需要获取控制器名称的节点id
     * @return str $controller
     */
    public function getController(){        
        $controller= getNodeName($this->data['id'],'controller');
        ret_msg(0,$controller);
    }
    
    /*
     * 删除节点(单条删除，批量删除)
     * 
     * @param array $ids 需要删除的节点id（批量删除为多条）
     * @return bool
     */
    public function nodeDel(){
        if(count($this->data['id'])==0){
            ret_msg(10009);
        }
        //查询是否有次级节点存在
        $map=array(
            'p_id'  =>$this->data['id'],
            'status'=>1
        );
        if($this->nodeModel->getOneNode($map)){
            ret_msg(10010);
        }
        $condition=array(
            'id'    =>$this->data['id'],
            'status'=>0
        );
        if(!$this->nodeModel->nodeEdit($condition)){
            ret_msg(10008);
        }
        ret_msg(20002);
    }
}

